import Vue from 'vue';
import tpl from './tpl.html';

import WebBox from '../../component/WebBox/WebBox';
import Loading from '../../../grsc-base/component/Loading.vue';
import service from './service'
import Validator from '../../../grsc-base/component/Validator/main';
import parseUrl from '../../../grsc-base/util/parseUrl'
import lib from '../../common/lib'
import {REG_LINK_KEY} from '../../../grsc-base/common/constants'

export default Vue.extend({
    template: tpl,
    components: {
        WebBox,
        Validator,
        Loading
    },
    data() {
        return {
            isGet: true,
            isLoading: true,
            isRequesting: false,

            isLogin: true,   // 是否登陸
            hadGetPresendCode: '',  // 已經領取過的状态码
            isSumitBtn: true,  // 是否显示 确定按钮
            getSuccess: false, // 成功领取
        }
    },
    created() {
        console.log(window.CODE.SUCCESS)
        this.isGetPresend();
    },
    methods: {
        // 领取事件
        getPresned(){
            if(!this.isLogin || this.hadGetPresendCode.length){
                return false;
            }
            service.getPresned({}).done((req)=> {
                this.isGetCheckCode = req.code
                if(req.code === window.CODE.SUCCESS){
                    this.isSumitBtn = false;
                    this.getSuccess = true;
                }else if(req.code =='116100' || req.code === '116101'){
                    // 116100 不满足要求   116101 已经领过了
                    this.hadGetPresendCode = req.code;
                    this.isSumitBtn = false;
                }else{
                    window.error(req.msg);
                }
            }).fail((req) => {
                this.isRequesting = false;
                window.error(req.msg);
            }).always(() => {
                this.isRequesting = false;
            });;
        },
        // 判斷是否可領取
        isGetPresend(){
            service.isGetPresend({}).done((req)=> {
                if (req.code === window.CODE.SUCCESS) {
                    this.isLogin = true;
                    this.hadGetPresendCode = '';
                }else if(req.code == '105101' || req.code === '105102' || req.code === '105108'){
                    // 未登陆  需要登陆
                    setTimeout(()=>{
                        let fromPath = window.encodeURIComponent(window.location.href);
                        window.location.href = './login.html?from=' + fromPath;
                    },3000)
                    this.isLogin = false;
                    window.error(`未登录，3秒后自动跳转登陆页面`);
                }else if(req.code =='116100' || req.code === '116101'){
                //     // 116100 不满足要求   116101 已经领过了
                //     this.hadGetPresendCode = req.code;
                //     this.isSumitBtn = false;
                //     this.isLogin = false;
                }else{
                    // window.error(req.msg);
                }

            }).fail((req) => {
                window.error(req.msg);
            }).always(() => {
                console.log(33333)
                this.isLoading = false;
                this.isRequesting = false;
            });
        }
        
    }
});